namespace Maple.Branch.Settings.Providers
{
    using System.Threading.Tasks;
    using IServices;
    using Volo.Abp.Settings;

    public abstract class BranchSettingProvider : IBranchSettingProvider
    {
        public abstract string Name { get; }

        protected IBranchSettingStore SettingStore { get; }

        protected BranchSettingProvider(IBranchSettingStore settingStore)
        {
            SettingStore = settingStore;
        }

        public virtual async Task<string> GetOrNullAsync(SettingDefinition setting, string providerKey)
        {
            return await SettingStore.GetOrNullAsync(setting.Name, Name, NormalizeProviderKey(providerKey));
        }

        public virtual async Task SetAsync(SettingDefinition setting, string value, string providerKey)
        {
            await SettingStore.SetAsync(setting.Name, value, Name, NormalizeProviderKey(providerKey));
        }

        public virtual async Task ClearAsync(SettingDefinition setting, string providerKey)
        {
            await SettingStore.DeleteAsync(setting.Name, Name, NormalizeProviderKey(providerKey));
        }

        protected virtual string NormalizeProviderKey(string providerKey)
        {
            return providerKey;
        }
    }
}
